import javax.swing.*;
import java.awt.*;
import javax.imageio.*;
import java.awt.image.*;
import java.io.File;
import java.awt.event.*;

class GUI{
	BufferedImage image1,image2;
	BufferedImage[] up = new BufferedImage[7];
	BufferedImage[] down = new BufferedImage[7];
	JFrame f = new JFrame("hwllo test!");
	JPanel p1 = new JPanel();
	gui bgui = new gui();
	int x,y;
	int posX=0,posY=0;
	int index1=630,index2=630,index3=630;
	static int[] upIndex  = new int[7];
	static int[] downIndex = new int[7];
	public static elevator e1 = new elevator();
	public static elevator e2 = new elevator();
	public static elevator e3 = new elevator();
	public static floorButton floor = new floorButton();


	public void initGUI(){
		try
		{
			image1 = ImageIO.read(new File("img/a.png"));
			image2 = ImageIO.read(new File("img/b.png"));
			for(int i=1;i<=6;i++){
				up[i] = ImageIO.read(new File("img/up.png"));
				down[i] = ImageIO.read(new File("img/down.png"));
				upIndex[i] = 0;
				downIndex[i]  = 0;
			}
		}
		catch(Exception e){
			System.out.println("read image faile!");
		}
		f.setSize(1000,700);
		f.setVisible(true);
		f.add(bgui);
		bgui.repaint();
		f.addMouseListener(new click());

	}
	public void move(elevator e,int index)throws Exception{
		
		int x=e.getCurFloor()*100;
		System.out.println(x+"  repaint!!!  "+index);
		switch(index){
			case 1:
				if(x>index1) while(index1++<x){
					bgui.repaint();
					Thread.sleep(5);
				} 
				else while(index1-->x){
					bgui.repaint();
					Thread.sleep(5);
				} 
				break;
			case 2:
				if(x>index2) while(index2++<x){
					bgui.repaint();
					Thread.sleep(5);
				} 
					else while(index2-->x){
						bgui.repaint();
						Thread.sleep(5);
					} 

				break;
			case 3:
				if(x>index3) while(index3++<x){
					bgui.repaint();
					Thread.sleep(5);
				} 
					else while(index3-->x){
						bgui.repaint();
						Thread.sleep(5);
					} 

				break;
		}

	}
	public void setXY(int index){
		switch(index){
			case 12:upIndex[1] = 1;
				break;
			case 21:downIndex[2] = 1;
				break;
			case 22:upIndex[2] = 1;
				break;
			case 31:downIndex[3] = 1;
				break;
			case 32:upIndex[3] = 1;
				break;
			case 41:downIndex[4] = 1;
				break;
			case 42:upIndex[4] = 1;
				break;
			case 51:downIndex[5] = 1;
				break;
			case 52:upIndex[5] = 1;
				break;
			case 61:downIndex[6] = 1;
				break;
			case 62:upIndex[6] = 1;
				break;
		}
		try{
			dispatch(index);
		}
		catch(Exception xxx){

		}
		
	}

	public void dispatch(int x) throws Exception{
		person p = new person(x);
		int ele1 = e1.isNeedStay(x);
		int ele2 = e2.isNeedStay(x);
		int ele3 = e3.isNeedStay(x);
		if(ele1==0) floor.callOut(p,e1);
		else if(ele2==0) {floor.callOut(p,e2);move(e2,2);}
		else if(ele3==0) {floor.callOut(p,e3);move(e3,3);}
		else if(ele1==-1) {floor.callOut(p,e1);move(e1,1);}
		else if(ele2==-1) {floor.callOut(p,e2);move(e2,2);}
		else if(ele3==-1) {floor.callOut(p,e3);move(e3,3);}

	}

	public static void main(String[] args)throws Exception{
		GUI gui = new GUI();
		gui.initGUI();
		Thread.sleep(1000);
	}

	class gui extends JPanel{
		public void paint(Graphics g){
				g.drawImage(image2,150,80,700,610,this);
				g.drawImage(image1,230,index1,40,50,this);
				g.drawImage(image1,430,index2,40,50,this);
				g.drawImage(image1,630,index3,40,50,this);
				
			for(int i=1;i<7;i++){
				g.drawImage(up[i],770,90+100*(i-1),35,40,null);
				g.drawImage(down[i],770,130+100*(i-1),35,40,null);
			}
		
		}
	}

	class click extends MouseAdapter{
		public void mouseClicked(MouseEvent e){
			x = (int)(e.getX());
			y = (int)(e.getY());
			if(x>=770 && x<=805){
				if(y>=91 && y<=127)  setXY(62);
				if(y>127 && y<=166)  setXY(61);

				if(y>=191 && y<=227) setXY(52);
				if(y>227 && y<=266)  setXY(51);

				if(y>=291 && y<=327) setXY(42);
				if(y>327 && y<=366)  setXY(41);

				if(y>=391 && y<=427) setXY(32);
				if(y>427 && y<=466)  setXY(31);

				if(y>=491 && y<=527) setXY(22);
				if(y>527 && y<=566)  setXY(21);

				if(y>=591 && y<=627) setXY(12);
			}
		}
	}

}

